package com.huqing.icu.rentreport.service.admin.impl;

import com.huqing.icu.base.PagingDto;
import com.huqing.icu.enums.DeleteStatusEnum;
import com.huqing.icu.rentreport.dto.ReportTypeConfigQueryDto;
import com.huqing.icu.rentreport.inout.in.admin.AdminReportTypeConfigListQueryReq;
import com.huqing.icu.rentreport.inout.in.admin.AdminReportTypeConfigSaveReq;
import com.huqing.icu.rentreport.mapper.ReportTypeConfigMapper;
import com.huqing.icu.rentreport.model.ReportTypeConfig;
import com.huqing.icu.rentreport.service.admin.IAdminReportTypeConfigService;
import com.huqing.icu.utils.CopyUtils;
import com.huqing.icu.utils.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @Description 后台举报记录配置实现类
 * @Author huqing
 * @Date 2025/7/6 17:12
 **/
@Service
public class AdminReportTypeConfigServiceImpl implements IAdminReportTypeConfigService {

    @Resource
    private ReportTypeConfigMapper reportTypeConfigMapper;

    /**
     * 新增或修改举报类型配置
     *
     * @param saveReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer save(AdminReportTypeConfigSaveReq saveReq, Long userId) {
        ReportTypeConfig reportTypeConfig = CopyUtils.copy(saveReq, ReportTypeConfig.class);
        if (reportTypeConfig.getId() == null) {
            reportTypeConfig.setCreateBy(userId);
            reportTypeConfigMapper.insert(reportTypeConfig);
        } else {
            reportTypeConfig.setModifyBy(userId);
            reportTypeConfigMapper.updateById(reportTypeConfig);
        }
        return 1;
    }

    /**
     * 查询举报类型列表
     *
     * @param queryReq
     * @return
     */
    @Override
    public PagingDto<ReportTypeConfig> queryList(AdminReportTypeConfigListQueryReq queryReq) {
        ReportTypeConfigQueryDto queryDto = CopyUtils.copy(queryReq, ReportTypeConfigQueryDto.class);
        int startIndex = PageUtils.getPageStartIndex(queryReq.getPageIndex(), queryReq.getPageSize());
        queryDto.setStartIndex(startIndex);

        //2、查询满足条件有多少个
        queryDto.setIsDeleted(DeleteStatusEnum.NOT_DELETED.getCode());
        Integer count = reportTypeConfigMapper.getNumByCondition(queryDto);
        if (count == 0) {
            return new PagingDto(Collections.emptyList(), 0, 0);
        }

        //3、根据条件查询举报类型列表
        List<ReportTypeConfig> reportTypeConfigList = reportTypeConfigMapper.selectByCondition(queryDto);
        PagingDto pagingDto = new PagingDto<>(reportTypeConfigList, PageUtils.getTotalPage(count, queryReq.getPageSize()), count);
        return pagingDto;
    }


    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delete(List<Long> idList) {

        //逻辑删除
        reportTypeConfigMapper.deleteByIdList(idList);
        return 1;
    }
}
